package cron.core;

import cron.core.loader.JavaStrTaskResourceLoader;
import cron.core.scheduler.TriggerScheduler;
import cron.core.scheduler.timer.TimeWheel;
import cron.core.scheduler.trigger.TriggerType;
import cron.task.plugin.Task;

/**
 * @author : willian fu
 * @version : 1.0
 * @date : 2021/3/2 0002 09:44
 */
public class JavaStrCompiler {
    public static void main(String[] args) throws ClassNotFoundException,
            IllegalAccessException, InstantiationException {
        String className = "mypackage.MyClass";
        String javaCode = "import cron.task.plugin.Task;\n" +
                "public class MyClass implements Task, Runnable {\n" +
                "    public void doRun() {\n" +
                "        System.out.println(\"Hello World\");\n" +
                "    }\n" +
                "    public void run() {\n" +
                "        Task t = new MyClass(); t.doRun();" +
                "    }\n" +
                "}\n";
        //创建一个java字符串加载器
        JavaStrTaskResourceLoader loader = new JavaStrTaskResourceLoader(className, javaCode);
        //
        Task[] instances = loader.loadTaskInstance(className);
        TriggerScheduler triggerScheduler = new TriggerScheduler(new TimeWheel().start());
        for (Task instance : instances) {
            String id = triggerScheduler.register(instance, TriggerType.FIX_RATE, 8);
            triggerScheduler.enable(id);
        }

    }


}
